package com.cysnake.quitsmoke.common.dao;

import java.text.ParseException;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.cysnake.quitsmoke.VO.HistoryVO;
import com.cysnake.quitsmoke.tools.FormatTools;

public class HistoryDao extends BaseDao {

	public static final String KEY_ID = "id";
	public static final String KEY_SMOKETIME = "smokeTime";
	public static final String TABLE_NAME = "history";

	private static final String TAG = "HistoryDao";
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;
	private final Context mCtx;

	public HistoryDao(Context ctx) {
		this.mCtx = ctx;
	}

	public HistoryDao open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public long create(HistoryVO history) {
		ContentValues initialValues = new ContentValues();
		if (history.getSmokeTime() != null) {
			initialValues.put(KEY_SMOKETIME,
					FormatTools.dateToString(history.getSmokeTime()));
		}
		return mDb.insert(TABLE_NAME, null, initialValues);
	}

	public boolean deleteHistoryById(long rowId) {

		return mDb.delete(TABLE_NAME, KEY_ID + " = " + rowId, null) > 0;
	}

	public ArrayList<HistoryVO> getAllHistory() {
		Cursor cursor = mDb.query(TABLE_NAME, new String[] { KEY_ID,
				KEY_SMOKETIME }, null, null, null, null, null);
		return changeCursor(cursor);

	}

	public HistoryVO getHistoryById(long rowId) throws SQLException {

		Cursor mCursor = mDb.query(true, TABLE_NAME, new String[] { KEY_ID,
				KEY_SMOKETIME }, KEY_ID + "=" + rowId, null, null, null, null,
				null);
		ArrayList<HistoryVO> list = changeCursor(mCursor);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}

	}

	public boolean update(HistoryVO history) {
		ContentValues args = new ContentValues();
		if (history.getSmokeTime() != null) {
			args.put(KEY_SMOKETIME,
					FormatTools.dateToString(history.getSmokeTime()));
		}
		return mDb.update(TABLE_NAME, args, KEY_ID + "=" + history.getId(),
				null) > 0;
	}

	public int deleteAll() {
		int i = mDb.delete(TABLE_NAME, null, null);
		return i;
	}

	private ArrayList<HistoryVO> changeCursor(Cursor cursor) {
		ArrayList<HistoryVO> list = new ArrayList<HistoryVO>();
		while (cursor.moveToNext()) {
			HistoryVO history = new HistoryVO();
			int temp = cursor.getColumnIndex(KEY_ID);
			history.setId(cursor.getInt(temp));
			temp = cursor.getColumnIndex(KEY_SMOKETIME);
			try {
				if(cursor.getString(temp)!=null){
					history.setSmokeTime(FormatTools.stringToDate(cursor
							.getString(temp)));
				}else{
					history.setSmokeTime(null);
				}
					
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			list.add(history);
		}
		return list;
	}
}
